﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CutAways.Illustration;

namespace CutAways
{
	partial class ModelViewpointsEditorForm : Form
	{
		private MyGLControl myGLControl;

		public ModelViewpointsEditorForm(MyGLControl myGLControl)
		{
			InitializeComponent();
			this.myGLControl = myGLControl;
			refresh();
		}

		private void refresh()
		{
			int viepointCount = myGLControl.Model.ViewpointSet.Count;
			comboBoxViewpoints.Items.Clear();
			for (int i = 0; i < viepointCount; i++)
				comboBoxViewpoints.Items.Add("Viewpoint " + (i + 1));
			comboBoxViewpoints.SelectedIndex = comboBoxViewpoints.Items.Count - 1;
			if (comboBoxViewpoints.Items.Count == 0)
			{
				buttonRemove.Text = "Remove";
				buttonRemove.Enabled = buttonOcclusionGraph.Enabled = false;
			}
		}

		private void buttonClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void comboBoxViewpoints_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (comboBoxViewpoints.SelectedIndex >= 0)
			{
				myGLControl.SetCamera(myGLControl.Model.ViewpointSet[comboBoxViewpoints.SelectedIndex].Clone());
				buttonRemove.Enabled = buttonOcclusionGraph.Enabled = true;
				buttonRemove.Text = "Remove viepoint " + (comboBoxViewpoints.SelectedIndex + 1);
			}
		}

		private void buttonAdd_Click(object sender, EventArgs e)
		{
			Cursor mem = this.Cursor;
			try
			{
				this.Cursor = Cursors.WaitCursor;
				myGLControl.Model.ViewpointSet.AddViewpoint(myGLControl.Model, myGLControl.Camera.Clone());
				refresh();
			}
			finally
			{
				this.Cursor = mem;
			}
		}

		private void buttonRemove_Click(object sender, EventArgs e)
		{
			if (comboBoxViewpoints.SelectedIndex >= 0)
			{
				myGLControl.Model.ViewpointSet.RemoveViewpointAt(comboBoxViewpoints.SelectedIndex);
				refresh();
			}
		}

		private void buttonOcclusionGraph_Click(object sender, EventArgs e)
		{
			if(comboBoxViewpoints.SelectedIndex >= 0)
			{
				OcclusionGraphVisualizationForm form = new OcclusionGraphVisualizationForm(myGLControl.Model.ViewpointSet.GetOcclusionGraphAt(comboBoxViewpoints.SelectedIndex));
				form.Show();
			}
		}
	}
}
